# /usr/bin/python3

# Author: 爱编程的章老师
# @Time: 2021/1/8 0008
# E-mail: Bluesand2010@163.com


'''第九课的作业分析'''
from random import randint

import pygame
import sys


# 第一题
def homework1_1():
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    pos = event.pos
                    r = randint(0, 255)
                    g = randint(0, 255)
                    b = randint(0, 255)
                    c = r, g, b
                    s.fill((0, 0, 0))
                    pygame.draw.circle(s, c, pos, 50, width=1)
                    pygame.display.update()
        # 处理组合关闭游戏
        keys = pygame.key.get_pressed()
        if keys[pygame.LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
            sys.exit()


def homework1_2():
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        # 处理组合关闭游戏
        keys = pygame.key.get_pressed()
        if keys[pygame.LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
            sys.exit()

        # 处理鼠标事件
        mouse = pygame.mouse.get_pressed()  # 获取鼠标点击状态元组 三个元素分别代表左键 ,中键 ,右键的点击状态.点了就是True否则为False
        if mouse[0]:  # 判断左键有没有点击
            pos = pygame.mouse.get_pos()  # 点击了左键就获取鼠标的位置作为圆的圆心
            r = randint(0, 255)
            g = randint(0, 255)
            b = randint(0, 255)
            c = r, g, b
            s.fill((0, 0, 0))
            pygame.draw.circle(s, c, pos, 50, width=1)
            pygame.display.update()


def homework2():
    pygame.init()
    s = pygame.display.set_mode((800, 600))
    rect = pygame.Rect(350, 250, 100, 100)
    color = 0, 255, 0
    pygame.draw.rect(s, color, rect, width=1)
    pygame.display.update()
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        mouse = pygame.mouse.get_pressed()  # 获取鼠标点击状态元组 三个元素分别代表左键 ,中键 ,右键的点击状态.点了就是True否则为False
        if mouse[0]:  # 判断左键有没有点击
            pos = pygame.mouse.get_pos()  # 点击了左键就获取鼠标的位置作为圆的圆心
            while rect.collidepoint(pos[0], pos[1]):
                rect.left = randint(0, 700)
                rect.top = randint(0, 500)
            s.fill((0, 0, 0))
            pygame.draw.rect(s, color, rect, width=1)
            pygame.display.update()

        # 处理组合关闭游戏
        keys = pygame.key.get_pressed()
        if keys[pygame.LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
            sys.exit()

def homework4():
    pygame.init()
    s = pygame.display.set_mode((800, 600))
    rect = pygame.Rect(350, 250, 100, 100)
    color = 0, 255, 0
    pygame.draw.rect(s, color, rect, width=1)
    pygame.display.update()
    mouse_flag = False # 鼠标第一次点击的标识
    have_rect = False  # 有没有复制的标记
    can_copy = False  # 能不能复制的标记
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN and not have_rect:
                if event.button == 1:
                    mouse_flag = True
                    start_pos = event.pos
            if event.type == pygame.MOUSEMOTION and mouse_flag:
                end_pos = event.pos
                left = start_pos[0] if start_pos[0] < end_pos[0] else end_pos[0]
                top = start_pos[1] if start_pos[1] < end_pos[1] else end_pos[1]
                width = abs(start_pos[0] - end_pos[0])
                height = abs(start_pos[1] - end_pos[1])
                new_rect = pygame.Rect(left, top, width, height)
                s.fill((0, 0, 0))
                pygame.draw.rect(s, color, rect, width=1)
                pygame.draw.rect(s, color, new_rect, width=1)
                pygame.display.update()
            if event.type == pygame.MOUSEBUTTONUP and mouse_flag:
                if event.button == 1:
                    mouse_flag = False
                    if new_rect.contains(rect):
                        can_copy = True
                        s.fill((0, 0, 0))
                        pygame.draw.rect(s, (255, 0, 0), rect, width = 5)  # 框选成功加粗显示
                        pygame.display.update()
                keys = pygame.key.get_pressed()
                if keys[pygame.K_LCTRL] or keys[pygame.K_RCTRL] and keys[pygame.K_v] and can_copy:
                    copy_rect = rect.move(0,0)
                    can_copy = False
                    have_rect = True
            if event.type == pygame.MOUSEBUTTONDOWN and have_rect:
                if event.button == 1:
                    have_rect = False
                    pos = event.pos
                    copy_rect.center = pos
                    s.fill((0, 0, 0))
                    pygame.draw.rect(s, color, rect, width=1)
                    pygame.draw.rect(s, color, copy_rect, width=1)
                    pygame.display.update()


        # 处理组合关闭游戏
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
            sys.exit()

if __name__ == '__main__':
    homework4()
